package com.cncytech.uiautomator_demo.page;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject;
import androidx.test.uiautomator.UiObject2;
import androidx.test.uiautomator.UiSelector;
import androidx.test.uiautomator.Until;

import java.util.Objects;

public class SettingPage {
    private UiDevice mDevice;
    private Context context;

    public SettingPage(UiDevice mDevice,Context context) {
        this.mDevice = mDevice;
        this.context = context;
    }
    public  void lancherApp(String pack,String mainActivity){
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(pack);

        if (Objects.nonNull(intent)) {
            // Clear out any previous instances
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        } else {
            intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//可选
            ComponentName comp = new ComponentName(pack, mainActivity);
            intent.setComponent(comp);
        }

        context.startActivity(intent);

        // Wait for the app to appear
        mDevice.wait(Until.hasObject(By.pkg(pack).depth(0)), 5000);
    }

    public void lanchSettingApp(){
        lancherApp("com.android.settings","com.android.settings.HWSettings");
    }

    public void OpenWlanSetting(){
        UiObject2 wlan = mDevice.findObject(By.textContains("WLAN"));
        if (wlan != null) {
            wlan.click();
        }
    }
    public UiObject findPower(){
        return mDevice.findObject(new UiSelector().resourceId("com.android.settings:id/switch_widget"));
    }
}
